package com.debao.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.debao.system.mapper.HarvestAddressMapper;
import com.debao.system.domain.HarvestAddress;
import com.debao.system.service.IHarvestAddressService;

/**
 * 用户收获地址Service业务层处理
 *
 * @author DeBao
 * @date 2024-06-12
 */
@Service
public class HarvestAddressServiceImpl implements IHarvestAddressService {
    @Autowired
    private HarvestAddressMapper harvestAddressMapper;

    /**
     * 查询用户收获地址
     *
     * @param id 用户收获地址主键
     * @return 用户收获地址
     */
    @Override
    public HarvestAddress selectHarvestAddressById(Long id) {
        return harvestAddressMapper.selectHarvestAddressById(id);
    }


    /**
     * 查询用户收获地址
     *
     * @param userId
     * @return
     */
    @Override
    public HarvestAddress selectDefaultHarvestAddressByUserId(Long userId) {
        return harvestAddressMapper.selectDefaultHarvestAddressByUserId(userId);
    }


    /**
     * 查询用户收获地址列表
     *
     * @param harvestAddress 用户收获地址
     * @return 用户收获地址
     */
    @Override
    public List<HarvestAddress> selectHarvestAddressList(HarvestAddress harvestAddress) {
        return harvestAddressMapper.selectHarvestAddressList(harvestAddress);
    }

    /**
     * 新增用户收获地址
     *
     * @param harvestAddress 用户收获地址
     * @return 结果
     */
    @Override
    public int insertHarvestAddress(HarvestAddress harvestAddress) {
        Boolean have = harvestAddressMapper.haveDefaultHarvestAddress(harvestAddress.getUserId());
        // 如果用户没有默认地址，则设置为默认地址
        if (!have) {
            harvestAddress.setIsDefault(1);
        }
        //如果用户已经有默认地址了，设置了当前地址为默认地址
        if (have && harvestAddress.getIsDefault() == 1) {
            // 设置该用户的全部地址为非默认地址
            harvestAddressMapper.setUnDefaultAddress(harvestAddress.getUserId());
        }
        return harvestAddressMapper.insertHarvestAddress(harvestAddress);
    }

    /**
     * 修改用户收获地址
     *
     * @param harvestAddress 用户收获地址
     * @return 结果
     */
    @Override
    public int updateHarvestAddress(HarvestAddress harvestAddress) {
        Boolean have = harvestAddressMapper.haveDefaultHarvestAddress(harvestAddress.getUserId());
        // 如果用户没有默认地址，则设置为默认地址
        if (!have) {
            harvestAddress.setIsDefault(1);
        }
        //如果用户已经有默认地址了，设置了当前地址为默认地址
        if (have && harvestAddress.getIsDefault() == 1) {
            // 设置该用户的全部地址为非默认地址
            harvestAddressMapper.setUnDefaultAddress(harvestAddress.getUserId());
        }
        return harvestAddressMapper.updateHarvestAddress(harvestAddress);
    }

    /**
     * 批量删除用户收获地址
     *
     * @param ids 需要删除的用户收获地址主键
     * @return 结果
     */
    @Override
    public int deleteHarvestAddressByIds(Long[] ids) {
        return harvestAddressMapper.deleteHarvestAddressByIds(ids);
    }

    /**
     * 删除用户收获地址信息
     *
     * @param id 用户收获地址主键
     * @return 结果
     */
    @Override
    public int deleteHarvestAddressById(Long id) {
        return harvestAddressMapper.deleteHarvestAddressById(id);
    }
}
